package com.imc.api.attence.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Sets;
import com.imc.api.attence.dao.ISignMapper;
import com.imc.api.attence.dao.repository.SignRepository;
import com.imc.api.attence.pojo.Sign;
import com.imc.core.utils.DateUtils;
import com.imc.core.utils.MysqlUtil;
import com.imc.platform.base.user.dao.mapper.IUserMapper;
import com.imc.platform.base.user.dao.repository.UserRepository;
import com.imc.platform.base.user.pojo.User;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/*
 * 类描述：签到服务类
 * 开发者：tanweiping
 * 创建日期：2018/10/28 21:02
 */
@Transactional
@Slf4j
@Service
public class SignService {

    @Autowired
    UserRepository userRepository;//用户操作仓库

    @Autowired
    IUserMapper iUserMapper;//用户mapper

    @Autowired
    ISignMapper signMapper;//签到mapper

    @Autowired
    SignRepository signRepository;//签到操作仓库

    @Autowired
    MysqlUtil mysqlUtil;//数据库操作类

    /*
     * OrderMapper
     */
    public List <Map> getStudentByLessonId(String schduleId) {
        val data = iUserMapper.queryUsersByLessonId(schduleId);
        if (data == null || data.isEmpty()) return new ArrayList <>();
        data.forEach(
                s -> {
                    if (s.containsKey("signStatus")) {
                        Integer signStatus = Integer.valueOf(s.get("signStatus").toString());
                        if (signStatus.equals(1)) {
                            s.put("checked", true);
                        } else {
                            s.put("checked", false);
                        }
                    } else {
                        s.put("checked", false);
                    }
                }
        );
        return data;
    }

    /*
     * YT  2018/10/28 21:03
     * 方法描述：发起签到
     */
    public void postAndPutSign(String scheduleId, List <Sign> signList, Principal principal) {
        //if (signList.isEmpty())return;
        List <Sign> existSign = signRepository.findAllByScheduleId(scheduleId);

        List <Integer> db = existSign.stream().filter(s -> s.getId() != null).map(Sign::getId).collect(Collectors.toList());
        List <Integer> hold = signList.stream().filter(s -> s.getId() != null).map(Sign::getId).collect(Collectors.toList());

        List <Integer> diff = Sets.difference(Sets.newHashSet(db), Sets.newHashSet(hold)).stream().collect(Collectors.toList());

        if (!diff.isEmpty()) {
            signRepository.deleteByIds(diff);
        }

        User user = mysqlUtil.getUserByLoginNameCommon(principal.getName());
        String d = DateUtils.getCurrentDate();
        signList.forEach(
                s -> {
                    if (s.getId() == null) {
                        s.setCreateTime(d);
                        s.setSignerId(String.valueOf(user.getId()));
                    } else {
                        Sign s_ = signRepository.findOne(s.getId());
                        s.setCreateTime(s_.getCreateTime());
                        s.setSignerId(s.getSignerId());
                    }
                    s.setStatus(1);
                    s.setUpdateTime(d);
                }
        );
        signRepository.save(signList);
    }
}